require(["./config"], () => {
    require(["jquery", "template", "request", "indexSwiper", "header", "nav", "footer"], ($, template, req, IndexSwiper, HeaderRepeat) => {
        class IndexPage{
            constructor() {
                this.init();
            }
            init() {

                // 获取页面数据并渲染
                this.pageInit()
                    .then(() => {
                        // 设置页面js效果
                        this.jsInit();
                    })
                
            }
            // 获取页面数据并渲染
            pageInit() {
                new HeaderRepeat();
                // home数据（活动区区域、轮播图区域）
                return req.homeData()
                    .then((res) => {
                        return new Promise((resolve) => {
                            // console.log(res);
                            if (res.code === 0) {
                                // console.log(res.data);
                                // 活动区数据
                                this.dataActivities = res.data.home_activities;
                                // console.log(this.dataActivities)
                                // 活动区渲染
                                decorateActivities(this.dataActivities);
        
                                // 轮播图数据
                                this.dataBanner = res.data.home_carousel;
                                // console.log(this.dataBanner);
                                // 轮播图渲染
                                new IndexSwiper(this.dataBanner);
    
                                // 热销区数据
                                this.dataHottest = res.data.home_hot;
                                // console.log(this.dataHottest);
                                decorateHottest(this.dataHottest);
                                
                                // 产品区数据
                                this.dataFloors = res.data.home_floors;
                                // console.log(this.dataFloors);
                                decorateFloors(this.dataFloors);

                                // 论坛区数据
                                this.dataForum = res.data.home_forum;
                                // console.log(this.dataForum);
                                decorateForum(this.dataForum);

                                resolve();
                            }
                        })
                    })
                // 渲染活动区方法
                function decorateActivities(dataList) {
                    // console.log(dataList);
                    // console.dir(template);
                    const htmlStr = template("activitiesTemplate", {data : dataList});
                    // console.log(htmlStr);
                    $("#activitiesTemplateBox").html(htmlStr);
                }
                // 渲染热销区方法
                function decorateHottest(dataList) {
                    const htmlStr = template("hottestTemplate", {data : dataList});
                    // console.log(htmlStr);
                    $("#hottestTemplateBox").html(htmlStr);
                }
                // 渲染产品区方法
                function decorateFloors(dataList) {
                    const htmlStr = template("floorsTemplate", {data : dataList});
                    // console.log(htmlStr);
                    $("#floorsTemplateBox").html(htmlStr);
                }
                // 渲染论坛区方法
                function decorateForum(dataList) {
                    const htmlStr = template("forumTemplate", {data : dataList})
                    $("#forumTemplateBox").html(htmlStr);
                }
            }
            jsInit() {

                $(".hottest .right")
                .click(function () {
                    if($(this).hasClass("active")) {
                        $(this)
                        .removeClass("active")
                        .prev()
                        .addClass("active")
                        $(".hottest .card-box-content")
                        .animate({"left" : "-1220px"}, 1000)
                        // .css("left", "-1220px");
                    }
                })
                $(".hottest .left")
                .click(function () {
                    if($(this).hasClass("active")) {
                        $(this)
                        .removeClass("active")
                        .next()
                        .addClass("active")
                        $(".hottest .card-box-content")
                        .animate({"left" : "0"}, 1000)
                        // .css("left", "0");
                    }
                })

                // 项目说明按钮
                $("#projectBtn")
                .click(function () {
                    let scrollY = $(window).scrollTop()
                    // console.log(scrollY);
                    $(this).nextAll("#projectMsg")
                    .css({
                        "display": "block",
                        "top": scrollY
                    });
                    $("body").css("overflow", "hidden")
                })
                $("#projectMsg .returnBtn")
                .click(function () {
                    $(this).parents("#projectMsg").css("display", "none");
                    $("body").css("overflow", "auto")
                })
                $("#projectMsg .returnStationBtn")
                .click(function () {
                    window.open("http://101.201.68.146", "_self")
                })
                $("#projectMsg .clearBtn")
                .click(function () {
                    localStorage.removeItem("userData");
                    localStorage.removeItem("cartData");
                    localStorage.removeItem("currentUser");
                    location.reload();
                })
            }
            
        }
        new IndexPage();
    })
})